/*
 * @(#)CommonDaoImpl.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */


package com.bis.common.dao.ibatis;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.bis.common.dao.CommonDao;
import com.bis.common.entity.CodeEntity;
import com.bis.common.entity.SystemEntity;
import com.bis.framework.base.dao.ibatis.IbatisDaoSupport;
import com.bis.framework.base.entity.BaseEntity;
import com.bis.framework.entity.UserEntity;
import com.bis.framework.util.common.CommonSqlUtil;
import com.bis.framework.util.common.Page;
import com.bis.framework.util.common.PageData;
import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
 * CommonDaoImpl
 * 
 * @author BIS 2012-12-24
 */
public class CommonDaoImpl extends IbatisDaoSupport<BaseEntity> implements CommonDao
{

    /**
     * SQL Name Space
     */
    private String    nameSpace;

    /**
     * SQL Name ID
     */
    private String    nameSpaceAndId;


    /**
     * @return the nameSpace
     */
    public String getNameSpace()
    {
        return nameSpace;
    }

    /**
     * @param nameSpace the nameSpace to set
     */
    public void setNameSpace(String nameSpace)
    {
        if (nameSpace.contains("."))
        {
            this.nameSpace = nameSpace;
        }
        else
        {
            this.nameSpace = nameSpace + ".";
        }
    }

    /**
     * @return the nameSpaceAndId
     */
    public String getNameSpaceAndId()
    {
        return nameSpaceAndId;
    }

    /**
     * @param nameSpaceAndId the nameSpaceAndId to set
     */
    public void setNameSpaceAndId(String nameSpaceAndId)
    {
        this.nameSpaceAndId = nameSpaceAndId;
    }

    /**
     * saveObj
     */
    public void saveObj(BaseEntity sourceEntity) throws Exception
    {
        getSqlMapClientTemplate().insert("Common.commonInsert", CommonSqlUtil.buildInsertSql(sourceEntity));
    }

    /**
     * saveObj
     */
    public void saveObj(final List<BaseEntity> lstObj) throws Exception
    {
        if (lstObj == null || lstObj.size() == 0)
        {
            return;
        }
        getSqlMapClientTemplate().execute(new SqlMapClientCallback()
        {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
            {
                try
                {
                    executor.startBatch();
                    int size = lstObj.size();
                    for (int i = 0; i < size; i++)
                    {
                        executor.insert("Common.commonInsert", CommonSqlUtil.buildInsertSql(lstObj.get(i)));
                    }
                    executor.executeBatch();
                    return null;
                }
                catch (Exception e)
                {
                    logger.error("异常信息:", e);
                    throw new SQLException();
                }
            }
        });
    }

    /**
     * updateObj
     */
    public void updateObj(String sql) throws Exception
    {
        getSqlMapClientTemplate().update("Common.commonUpdate", sql);
    }

    /**
     * updateObj
     */
    public void updateObj(final List<BaseEntity> lstObj) throws Exception
    {
        if (lstObj == null || lstObj.size() == 0)
        {
            return;
        }
        getSqlMapClientTemplate().execute(new SqlMapClientCallback()
        {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
            {
                try
                {
                    executor.startBatch();
                    int size = lstObj.size();
                    for (int i = 0; i < size; i++)
                    {
                        executor.update("Common.commonUpdate", CommonSqlUtil.buildUpdateSqlForAllProperties(lstObj.get(i)));
                    }
                    executor.executeBatch();
                    return null;
                }
                catch (Exception e)
                {
                    logger.error("异常信息:", e);
                    throw new SQLException();
                }
            }
        });
    }

    /**
     * checkNameDuplication
     */
    public Integer checkNameDuplication(String sql)
    {
        return (Integer) this.getSqlMapClientTemplate().queryForObject("Common.checkNameDuplication", sql);
    }

    /**
     * selectForCompany
     */
    public List<SystemEntity> selectForCompany(String sql)
    {
        return (List<SystemEntity>) this.getSqlMapClientTemplate().queryForList("Common.selectForCompany", sql);
    }

    /**
     * deleteObjById
     */
    public void deleteObjById(String sql)
    {
        this.getSqlMapClientTemplate().delete("Common.deleteObjById", sql);
    }

    /**
     * deleteObjById
     */
    public void deleteObjById(final List<BaseEntity> lstObj) throws Exception
    {
        if (lstObj == null || lstObj.size() == 0)
        {
            return;
        }
        getSqlMapClientTemplate().execute(new SqlMapClientCallback()
        {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
            {
                try
                {
                    executor.startBatch();
                    int size = lstObj.size();
                    for (int i = 0; i < size; i++)
                    {
                        executor.delete("Common.deleteObjById", CommonSqlUtil.buildDeleteSql(lstObj.get(i)));
                    }
                    executor.executeBatch();
                    return null;
                }
                catch (Exception e)
                {
                    logger.error("异常信息:", e);
                    throw new SQLException();
                }
            }
        });
    }

    /**
     * selectSysCodeForCombox
     */
    public List<CodeEntity> selectSysCodeForCombox(String type)
    {
        return (List<CodeEntity>) this.getSqlMapClientTemplate().queryForList("Common.selectSysCodeForCombox", type);
    }

    /**
     * selectSendCompany
     */
    public List<SystemEntity> selectSendCompany(Map mpCondition)
    {
        return (List<SystemEntity>) this.getSqlMapClientTemplate().queryForList("Common.selectSendCompany", mpCondition);
    }

    /**
     * deleteCompanyByInfoId
     */
    public void deleteCompanyByInfoId(Map mpCondition)
    {
        this.getSqlMapClientTemplate().delete("Common.deleteCompanyByInfoId", mpCondition);
    }

    /**
     * deleteSendDocument
     */
    public void deleteSendDocument(final List lstDocument)
    {
        if (lstDocument == null || lstDocument.size() == 0)
        {
            return;
        }
        getSqlMapClientTemplate().execute(new SqlMapClientCallback()
        {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException
            {
                try
                {
                    executor.startBatch();
                    int size = lstDocument.size();
                    for (int i = 0; i < size; i++)
                    {
                        executor.delete("Common.deleteSendDocument", lstDocument.get(i));
                    }
                    executor.executeBatch();
                    return null;
                }
                catch (Exception e)
                {
                    logger.error("异常信息:", e);
                    throw new SQLException();
                }
            }
        });
    }

    /**
     * selectForUser
     */
    public List<UserEntity> selectForUser(String sql)
    {
        return (List<UserEntity>) this.getSqlMapClientTemplate().queryForList("Common.selectForUser", sql);
    }

    /**
     * selectForHandleUser
     */
    public List<UserEntity> selectForHandleUser(String sql)
    {
        return (List<UserEntity>) this.getSqlMapClientTemplate().queryForList("Common.selectForHandleUser", sql);
    }

    /**
     * selectByProc
     */
    public PageData selectByProc(Page page, Map map)
    {
        return this.selectByPagingProcedure(getNameSpaceAndId(), map, page);
    }

    /**
     * selectByProc
     */
    public List selectByProc(Map map)
    {
        return this.selectByNormalprocedure(getNameSpaceAndId(), map);
    }

}